import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QApplication, QInputDialog
from PyQt5.QtCore import QTimer
from light_control.search_device_thread import SearchDeviceThread
from light_control.set_command_thread import SetCommandThread
import random
import _thread
from socket import *
from light_control.config_dialog import ConfigDialog


class Config(QWidget):

    def __init__(self):
        super(Config, self).__init__()
        self.init_data()
        self.init_ui()
        self.show()

    def init_ui(self):
        main_layout = QVBoxLayout()
        self.create_container()
        self.create_operation()
        main_layout.addLayout(self.container)
        main_layout.addLayout(self.operate_layout)
        self.setLayout(main_layout)
        self.setWindowTitle('配置界面')
        self.resize(800, 600)

    def create_container(self):
        print('创建灯的容器')
        self.container = QHBoxLayout()

    def create_operation(self):
        self.operate_layout = QHBoxLayout()
        start_button = QPushButton('开始')
        start_button.setFixedSize(100, 50)
        start_button.clicked.connect(self.start_operate)

        search_button = QPushButton('搜索')
        search_button.setFixedSize(100, 50)
        search_button.clicked.connect(self.start_search)

        end_button = QPushButton('结束')
        end_button.setFixedSize(100, 50)
        end_button.clicked.connect(self.end_operate)

        self.operate_layout.addWidget(search_button)
        self.operate_layout.addWidget(start_button)
        self.operate_layout.addWidget(end_button)

    def start_operate(self):
        print('start_operate')
        dialog = ConfigDialog()
        if dialog.exec_():
            print(str(dialog.get_data()))
        # self.timer = QTimer()
        # self.timer.start(100)
        # self.timer.timeout.connect(self.send_color)

    def end_operate(self):

        if hasattr(self, 'timer'):
            self.timer.stop()
            print('结束')

    def start_search(self):
        self.lights.clear()
        self.search_thread.start()

    def search_message(self, addr):
        if addr not in self.lights.keys():
            self.lights[addr] = -1
            self.add_light(addr)

    def send_command_message(self, addr, message):
        self.command = SetCommandThread(addr, message)
        self.command.start()

    def init_data(self):
        self.lights = {}
        self.search_thread = SearchDeviceThread()
        self.search_thread.connect(self.search_message)
        self.orientation = ('左', '左上', '上', '右上', '右')

    def add_light(self, addr):
        print(addr)
        light = QPushButton('light' + str(len(self.lights)))
        light.setFixedSize(100, 50)
        self.container.addWidget(light)
        light.clicked.connect(lambda: self.adjust_position(addr, light))

    def adjust_position(self, addr, button):
        print(addr)
        _thread.start_new_thread(self.send_udp, (addr, 'led_breath'))
        item, okPressed = QInputDialog.getItem(self, "选择方位", "方位:", self.orientation, 0, False)
        self.lights[addr] = self.orientation.index(item)
        button.setText(item)

    def send_udp(self, addr, message):
        udp_socket = socket(AF_INET, SOCK_DGRAM)
        udp_socket.sendto(message.encode(), (addr, 6666))

    def send_color(self):
        self.timer.stop();
        print('send_color')
        for key, value in self.lights.items():
            message = 'set_rgb_bright_duration ' + str(random.randint(0, 255)) + " " + str(
                random.randint(0, 255)) + ' ' + str(random.randint(0, 255)) + ' 80 200'
            # self.send_command_message(key, message)
            _thread.start_new_thread(self.send_udp, (key, message))
        self.timer.start(100)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Config()
    sys.exit(app.exec_())
